package cn.t.repository.impl.sku;


import cn.t.converter.sku.CustomerSkuConverter;
import cn.t.core.mode.PageQueryDTO;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.sku.CustomerSkuDO;
import cn.t.persistence.entity.sku.CustomerSkuPO;
import cn.t.persistence.mapper.sku.CustomerSkuMapper;
import cn.t.repository.sku.CustomerSkuRepository;
import cn.t.request.CustomerSkuReq;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户商品映射表(CustomerSku)数据仓库
 *
 * @author t
 * @since 2024-05-24 10:18:08
 */
@Slf4j
@Repository
@AllArgsConstructor
public class CustomerSkuRepositoryImpl extends RepositoryImpl<CustomerSkuMapper, CustomerSkuPO, CustomerSkuConverter, CustomerSkuDO> implements CustomerSkuRepository {


    @Override
    public List<CustomerSkuDO> findByModel(CustomerSkuDO customerSkuDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize
                :
                " limit " + start + "," + pageSize;

        List<CustomerSkuPO> list = getWrapper(customerSkuDO)
                .last(lastSql)
                .list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<CustomerSkuPO> getWrapper(CustomerSkuDO customerSkuDO) {
        LambdaQueryChainWrapper<CustomerSkuPO> wrapper = lambdaQuery();
        if (ObjectUtils.isEmpty(customerSkuDO.getSkuCode())) {
            wrapper.eq(CustomerSkuPO::getSkuCode, customerSkuDO.getSkuCode());
        }
        if (ObjectUtils.isEmpty(customerSkuDO.getSkuName())) {
            wrapper.eq(CustomerSkuPO::getSkuName, customerSkuDO.getSkuName());
        }
        if (ObjectUtils.isEmpty(customerSkuDO.getFirmId())) {
            wrapper.eq(CustomerSkuPO::getFirmId, customerSkuDO.getFirmId());
        }
        if (ObjectUtils.isEmpty(customerSkuDO.getCustomerSkuCode())) {
            wrapper.eq(CustomerSkuPO::getCustomerSkuCode, customerSkuDO.getCustomerSkuCode());
        }
        if (ObjectUtils.isEmpty(customerSkuDO.getCustomerSkuName())) {
            wrapper.eq(CustomerSkuPO::getCustomerSkuName, customerSkuDO.getCustomerSkuName());
        }
        if (ObjectUtils.isEmpty(customerSkuDO.getGmtModified())) {
            wrapper.between(CustomerSkuPO::getGmtModified, customerSkuDO.getGmtModifiedStart(), customerSkuDO.getGmtModifiedEnd());
        }
        if (ObjectUtils.isEmpty(customerSkuDO.getGmtCreated())) {
            wrapper.between(CustomerSkuPO::getGmtCreated, customerSkuDO.getGmtCreatedStart(), customerSkuDO.getGmtCreatedEnd());
        }
        return wrapper;
    }

    @Override
    public long count(CustomerSkuDO customerSkuDO) {
        return getWrapper(customerSkuDO).count();
    }

    @Override
    public List<String> findSkuByCustomerCode(String customerSkuCode) {
        if (org.apache.commons.lang3.StringUtils.isBlank(customerSkuCode)) {
            return Collections.emptyList();
        }
        LambdaQueryChainWrapper<CustomerSkuPO> wrapper = lambdaQuery();
        wrapper.eq(CustomerSkuPO::getCustomerSkuCode, customerSkuCode);
        List<CustomerSkuPO> list = wrapper.list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list.stream().map(CustomerSkuPO::getSkuCode).collect(Collectors.toList());
    }

    @Override
    public void addBatch(List<CustomerSkuDO> customerSkuDos) {
        if (CollectionUtils.isEmpty(customerSkuDos)) {
            return;
        }
        List<CustomerSkuPO> customerSkuPOS = customerSkuDos.stream().map(this.converter::model2Po).collect(Collectors.toList());
        this.saveBatch(customerSkuPOS);
    }

    @Override
    public List<CustomerSkuDO> page(PageQueryDTO<CustomerSkuReq> req, String field, String desc) {
        LambdaQueryChainWrapper<CustomerSkuPO> wrapper = getWrapper(req.getParam());
        String lastSql = StringUtils.hasText(field) ?
                " order by " + field +" "+ desc + " limit " + req.getStart() + "," + req.getPageSize()
                :
                " limit " + req.getStart() + "," + req.getPageSize();

        List<CustomerSkuPO> list = wrapper.last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    @Override
    public Long queryCustomerSkuCount(CustomerSkuReq param) {
        LambdaQueryChainWrapper<CustomerSkuPO> wrapper = getWrapper(param);
        return wrapper.count();
    }

    public LambdaQueryChainWrapper<CustomerSkuPO> getWrapper(CustomerSkuReq reqParam) {
        LambdaQueryChainWrapper<CustomerSkuPO> wrapper = lambdaQuery();
        if (null != reqParam) {
            if (!ObjectUtils.isEmpty(reqParam.getSkuCode())) {
                wrapper.eq(CustomerSkuPO::getSkuCode, reqParam.getSkuCode());
            }
        }
        return wrapper;
    }
}
